package org.xlx.xlx_oa.warehouse.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.role_user.domain.AdmDepartment;
import org.xlx.xlx_oa.role_user.domain.AdmPosition;
import org.xlx.xlx_oa.role_user.domain.AdmUser;
import org.xlx.xlx_oa.role_user.mapper.AdmDepartmentMapper;
import org.xlx.xlx_oa.role_user.mapper.AdmPositionMapper;
import org.xlx.xlx_oa.role_user.mapper.AdmUserMapper;
import org.xlx.xlx_oa.warehouse.convertor.WarehouseConvertor;
import org.xlx.xlx_oa.warehouse.domain.Warehouse;
import org.xlx.xlx_oa.warehouse.domain.dto.WarehouseDto;
import org.xlx.xlx_oa.warehouse.domain.vo.WarehouseVo;
import org.xlx.xlx_oa.warehouse.mapper.WarehouseMapper;

import java.util.List;
import java.util.Objects;

@Service
public class WarehouseService {
    private final WarehouseConvertor warehouseConvertor = WarehouseConvertor.INSTANCE;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private WarehouseFacilityService facilityService;
    @Autowired
    private AdmUserMapper userMapper;
    @Autowired
    private AdmDepartmentMapper departmentMapper;
    @Autowired
    private AdmPositionMapper positionMapper;

    /**
     * 保存仓库
     */
    public Boolean saveWarehouse(WarehouseVo warehouseVo) {
        Long id = warehouseVo.getId();
        Warehouse warehouse = warehouseConvertor.convertVoToDomain(warehouseVo);
        if (Objects.isNull(id)) {
            warehouseMapper.insert(warehouse);
        } else {
            warehouseMapper.updateById(warehouse);
        }
        return true;
    }

    /**
     * 删除仓库
     */
    public Boolean removeWarehouse(Long id) {
        Boolean aBoolean = facilityService.checkWarehouseEmpty(id);
        if (aBoolean) {
            warehouseMapper.deleteById(id);
        } else {
            throw new BusinessException("100011", "The warehouse is not empty. Please empty the warehouse first");
        }
        return true;
    }

    /**
     * 仓库列表
     */
    public List<WarehouseDto> getList(WarehouseVo warehouseVo) {
        Integer pageNum = warehouseVo.getPageNum();
        Integer pageSize = warehouseVo.getPageSize();
        Page<Warehouse> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Warehouse> wrapper = new QueryWrapper<>();
        if (warehouseVo.getTitle() != null && (!"".equals(warehouseVo.getTitle()))) {
            wrapper.lambda().like(Warehouse::getTitle, warehouseVo.getTitle());
        }
        warehouseMapper.selectPage(page, wrapper);
        List<WarehouseDto> warehouseDtos = warehouseConvertor.convertToDomainDtoList(page.getRecords());
        warehouseDtos.forEach(warehouseDto -> this.supplyWarehouseDto(warehouseDto));
        return warehouseDtos;
    }

    /**
     * 仓库总数
     * @param warehouseVo
     * @return
     */
    public Integer getCount(WarehouseVo warehouseVo){
        QueryWrapper<Warehouse> wrapper = new QueryWrapper<>();
        if (warehouseVo.getTitle() != null && (!"".equals(warehouseVo.getTitle()))) {
            wrapper.lambda().like(Warehouse::getTitle, warehouseVo.getTitle());
        }
        return warehouseMapper.selectCount(wrapper);
    }

    /**
     * 模糊查询
     * @param title
     * @return
     */
    public  List<WarehouseDto> searchByTitle(String title){
        QueryWrapper<Warehouse> wrapper = new QueryWrapper<>();
        if (title != null && (!"".equals(title))) {
            wrapper.lambda().like(Warehouse::getTitle, title);
        }
        List<Warehouse> warehouses = warehouseMapper.selectList(wrapper);
        List<WarehouseDto> warehouseDtos = warehouseConvertor.convertToDomainDtoList(warehouses);
        warehouseDtos.forEach(warehouseDto -> this.supplyWarehouseDto(warehouseDto));
        return warehouseDtos;
    }

    /**
     * 补全计划
     *
     * @param warehouseDto
     */
    public void supplyWarehouseDto(WarehouseDto warehouseDto) {
        Long userId = warehouseDto.getUserId();
        AdmUser user = userMapper.selectById(userId);
        if (user.getDepartmentId() != 0 && user.getDepartmentId() != null) {
            AdmDepartment admDepartment = departmentMapper.selectById(user.getDepartmentId());
            warehouseDto.setDepartmentTitle(admDepartment.getTitle());
        }
        if (user.getPositionId() != 0 && user.getPositionId() != null) {
            AdmPosition position = positionMapper.selectById(user.getPositionId());
            warehouseDto.setPositionTitle(position.getTitle());
        }
        warehouseDto.setLastName(user.getLastName());
        warehouseDto.setMiddleName(user.getMiddleName());
        warehouseDto.setFirstName(user.getFirstName());
    }
}
